document.addEventListener('DOMContentLoaded', function() {
    const passwordInput = document.getElementById('passwordInput');
    const checkBtn = document.getElementById('checkBtn');
    const toggleBtn = document.getElementById('toggleVisibility');
    const strengthBar = document.getElementById('strengthBar');
    const strengthText = document.getElementById('strengthText');
    const suggestionsDiv = document.getElementById('suggestions');

    // 密码可见性切换
    toggleBtn.addEventListener('click', function() {
        if (passwordInput.type === 'password') {
            passwordInput.type = 'text';
            toggleBtn.textContent = '🙈';
        } else {
            passwordInput.type = 'password';
            toggleBtn.textContent = '👁️';
        }
    });

    // 检查密码强度
    checkBtn.addEventListener('click', checkPasswordStrength);
    passwordInput.addEventListener('input', checkPasswordStrength);

    function checkPasswordStrength() {
        const password = passwordInput.value;
        const suggestions = [];
        let strength = 0;

        // 重置UI
        document.querySelectorAll('.criteria li').forEach(li => {
            li.classList.remove('valid');
        });

        // 检查长度
        if (password.length >= 8) {
            strength++;
            document.getElementById('length').classList.add('valid');
        } else {
            suggestions.push("密码长度应至少为8个字符");
        }

        // 检查大写字母
        if (/[A-Z]/.test(password)) {
            strength++;
            document.getElementById('uppercase').classList.add('valid');
        } else {
            suggestions.push("添加至少一个大写字母(A-Z)");
        }

        // 检查小写字母
        if (/[a-z]/.test(password)) {
            strength++;
            document.getElementById('lowercase').classList.add('valid');
        } else {
            suggestions.push("添加至少一个小写字母(a-z)");
        }

        // 检查数字
        if (/[0-9]/.test(password)) {
            strength++;
            document.getElementById('number').classList.add('valid');
        } else {
            suggestions.push("添加至少一个数字(0-9)");
        }

        // 检查特殊字符
        if (/[^A-Za-z0-9]/.test(password)) {
            strength++;
            document.getElementById('special').classList.add('valid');
        } else {
            suggestions.push("添加至少一个特殊字符(如!@#$%^&*)");
        }

        // 更新强度条
        const percentage = (strength / 5) * 100;
        strengthBar.style.width = `${percentage}%`;

        // 设置颜色
        if (strength <= 1) {
            strengthBar.style.backgroundColor = '#e74c3c'; // 红色
            strengthText.textContent = "密码强度: 非常弱";
            strengthText.style.color = '#e74c3c';
        } else if (strength <= 3) {
            strengthBar.style.backgroundColor = '#f39c12'; // 橙色
            strengthText.textContent = "密码强度: 中等";
            strengthText.style.color = '#f39c12';
        } else {
            strengthBar.style.backgroundColor = '#2ecc71'; // 绿色
            strengthText.textContent = "密码强度: 强";
            strengthText.style.color = '#2ecc71';
        }

        // 显示建议
        if (suggestions.length > 0 && password.length > 0) {
            suggestionsDiv.innerHTML = '<h3>改进建议:</h3>' +
                suggestions.map(s => `<p>• ${s}</p>`).join('');
        } else if (password.length === 0) {
            suggestionsDiv.innerHTML = '';
            strengthText.textContent = "密码强度: 未检测";
            strengthText.style.color = '#333';
            strengthBar.style.width = '0%';
        } else {
            suggestionsDiv.innerHTML = '<p>✅ 密码强度优秀!</p>';
        }
    }
});